﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;

using System.Windows.Forms;
using System.Net;
using mshtml;
using System.IO;

namespace PriceWatcher
{
    public class Target:ListViewItem
    {
        private string name;
        private string strURL;
        private int index;
        private decimal price;
        public Target()
        {
            base.Text = "";//index 0, name
            base.SubItems.Add("");//index 1, price
            base.SubItems.Add("");//index 2, url
            base.SubItems.Add("");//index 3, index
        }

        public string TargetName
        {
            get { return name; }
            set
            {
                if (string.IsNullOrEmpty(value))
                {
                    throw new ApplicationException("目标名称不能为空");
                }
                else
                {
                    name = value;
                    base.Text = name;
                }
            }
        }

        public string TargetURL
        {
            get { return this.strURL; }
            set
            {
                if (string.IsNullOrEmpty(value))
                    throw new ApplicationException("目标名称不能为空");
                else if (value.StartsWith(@"http://") || value.StartsWith(@"https://"))
                {
                    strURL = value;
                    base.SubItems[2].Text =strURL;
                }
                else
                    throw new ApplicationException("网址无效");
            }
        }

        public int TargetIndex
        {
            get { return this.index; }
            set
            {
                if (value < 0)
                    throw new ApplicationException("目标对象不能为空");
                else
                {
                    index = value;
                    base.SubItems[3].Text =index.ToString();
                }
            }
        }

        public decimal TargetPrice
        {
            get { return this.price; }
            set {
                price = value;
                base.SubItems[1].Text = price.ToString();
            }
        }

        public void Write(XmlDocument xmlDoc, string xmlFileName)
        {
            XmlNode targets = xmlDoc.GetElementsByTagName("AllTargets")[0];
            XmlNode item = xmlDoc.CreateElement("Target");
            XmlElement node = xmlDoc.CreateElement("Name");
            node.InnerText = name;
            item.AppendChild(node);
            node = xmlDoc.CreateElement("URL");
            node.InnerText = strURL;
            item.AppendChild(node);
            node = xmlDoc.CreateElement("Index");
            node.InnerText = index.ToString();
            item.AppendChild(node);
            node = xmlDoc.CreateElement("Price");
            node.InnerText = price.ToString();
            item.AppendChild(node);
            targets.AppendChild(item);
            xmlDoc.Save(xmlFileName);
        }

        public void Read(XmlNode xmlTarget)
        {
            XmlNode node=xmlTarget.SelectSingleNode(@"./Name");
            name=node.InnerText;
            node = xmlTarget.SelectSingleNode(@"./Price");
            price = Convert.ToDecimal(node.InnerText);
            node = xmlTarget.SelectSingleNode(@"./URL");
            strURL = node.InnerText;
            node = xmlTarget.SelectSingleNode(@"./Index");
            index = Convert.ToInt32(node.InnerText);
            base.Text = name;
            base.SubItems[1].Text = price.ToString();
            base.SubItems[2].Text = strURL;
            base.SubItems[3].Text = index.ToString();
        }

        public void Edit(XmlNode xmlTarget, string xmlFileName)
        {
            XmlNode node = xmlTarget.SelectSingleNode(@"./Name");
            node.InnerText = name;
            node = xmlTarget.SelectSingleNode(@"./Price");
            node.InnerText = price.ToString();
            node = xmlTarget.SelectSingleNode(@"./URL");
            node.InnerText = strURL;
            node = xmlTarget.SelectSingleNode(@"./Index");
            node.InnerText = index.ToString();
            node.OwnerDocument.Save(Program.g_xmlFileName);
        }

        public void Watch()
        {
            WebRequest request = WebRequest.Create(this.strURL);
            WebResponse response = request.GetResponse();
            Stream stream = response.GetResponseStream();
            StreamReader sReader = new StreamReader(stream);
            string htmlCode = sReader.ReadToEnd().Trim();
            IHTMLDocument2 doc = new mshtml.HTMLDocument() as IHTMLDocument2;
            doc.write(htmlCode);
            IHTMLElementCollection eleCol = doc.all;
            IHTMLElement ele = eleCol.item(this.index);
            string tmp = ele.innerText;
            if (tmp == null) tmp = "0";
            System.Text.RegularExpressions.Regex reg = new System.Text.RegularExpressions.Regex(@"^[-+]?[0-9]*.?[0-9]+$");
            if (!reg.IsMatch(tmp)) tmp = "0";
            System.Diagnostics.Debug.WriteLine("in the watch: "+tmp);
        }
    }
}